package org.aceor.mddal.common.sqlparser.result.condition;

import com.google.common.base.Joiner;
import com.google.common.base.Objects;
import com.google.common.base.Strings;
import org.aceor.mddal.common.sqlparser.result.condition.value.BindingValue;

import java.util.List;

/**
 * Created by lxue on 16/3/23.
 */
public class ComparableUnit implements Comparable, Cloneable{

    private Comparable value;
    private BinaryOperator binaryOperator;
    protected List<Object> args;

    public ComparableUnit() {
    }

    public ComparableUnit(Comparable value, BinaryOperator binaryOperator) {
        this.value = value;
        this.binaryOperator = binaryOperator;
    }

    public ComparableUnit(Comparable value, BinaryOperator binaryOperator, List<Object> args) {
        this.value = value;
        this.binaryOperator = binaryOperator;
        this.args = args;
    }

    public Comparable getValue() {
        return value;
    }

    public void setValue(Comparable value) {
        this.value = value;
    }

    public BinaryOperator getBinaryOperator() {
        return binaryOperator;
    }

    public void setBinaryOperator(BinaryOperator binaryOperator) {
        this.binaryOperator = binaryOperator;
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(value, binaryOperator);
    }

    @Override
    public boolean equals(Object obj) {
        if (null == obj) {
            return false;
        }
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof ComparableUnit)) {
            return false;
        }
        ComparableUnit other = (ComparableUnit) obj;
        return Objects.equal(other.binaryOperator,binaryOperator) && Objects.equal(other.value, value);
    }

    @Override
    public int compareTo(Object o) {
        if (o instanceof ComparableUnit) {
            ComparableUnit obj = (ComparableUnit) o;
            return this.getValue().compareTo(obj.getValue());
        } else if (o instanceof Comparable) {
            return this.getValue().compareTo(o);
        }
        return -1;
    }

    @Override
    public String toString() {
        if (null == value) {
            return "";
        }
        return Joiner.on("").join("(",binaryOperator.getName(),value.toString(),")");
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return new ComparableUnit(this.value, this.binaryOperator);
    }

    public ComparableUnit fillValue() {
        if (null == args && value instanceof BindingValue) {
            throw new NullPointerException();
        }
        if (value instanceof BindingValue) {
            return new ComparableUnit(((BindingValue)value).fillValue(args), binaryOperator);
        } else if (value instanceof ComparableUnit) {
            return new ComparableUnit(((ComparableUnit) value).fillValue(), binaryOperator);
        } else {
            return new ComparableUnit(value, binaryOperator);
        }
    }

    public enum BinaryOperator {
        GREATER_THAN(1,">"),
        GREATER_THAN_OR_EQUAL(2,">="),
        EQUIVALENT(3,"="),
        LIKE(4,"LIKE"),
        NOT_LIKE(5,"NOT LIKE"),
        NOT_EQUIVALENT(6,"!="),
        LESS_THAN(7,"<"),
        LESS_THAN_OR_EQUAL(8,"<=")
        ;
        private int value;
        private String name;

        BinaryOperator(int value, String name) {
            this.value = value;
            this.name = name;
        }

        public static BinaryOperator valueof(String name) {
            if (Strings.isNullOrEmpty(name)) {
                throw new NullPointerException("BinaryOperator$valueOf==>param is null");
            }
            String nameUpperCase = name.toUpperCase();
            if (">".equals(nameUpperCase)) {
                return GREATER_THAN;
            } else if (">=".equals(nameUpperCase)) {
                return GREATER_THAN_OR_EQUAL;
            } else if ("=".equals(nameUpperCase)) {
                return EQUIVALENT;
            } else if ("LIKE".equals(nameUpperCase)) {
                return LIKE;
            } else if ("NOT LIKE".equals(nameUpperCase)) {
                return NOT_LIKE;
            } else if ("!=".equals(nameUpperCase)) {
                return NOT_EQUIVALENT;
            } else if ("<".equals(nameUpperCase)) {
                return LESS_THAN;
            } else if ("<=".equals(nameUpperCase)) {
                return LESS_THAN_OR_EQUAL;
            } else {
                throw new IllegalArgumentException("BinaryOperator$valueOf==>param is Illegal");
            }
        }

        public static BinaryOperator reverseBinaryOp(BinaryOperator op) {
            switch (op) {
                case GREATER_THAN:
                    return LESS_THAN;
                case GREATER_THAN_OR_EQUAL:
                    return LESS_THAN_OR_EQUAL;
                case LESS_THAN:
                    return GREATER_THAN;
                case LESS_THAN_OR_EQUAL:
                    return GREATER_THAN_OR_EQUAL;
                case EQUIVALENT:
                    return NOT_EQUIVALENT;
                case NOT_EQUIVALENT:
                    return EQUIVALENT;
                case LIKE:
                case NOT_LIKE:
                    throw new UnsupportedOperationException("LIKE,Not Like不支持reverse操作");
                default:
                    throw new IllegalArgumentException();
            }
        }
        public int getValue() {
            return value;
        }

        public String getName() {
            return name;
        }
    }
}
